package com.itheima.d3_collection_traverse;

import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Consumer;

/**
     目标：Collection集合的遍历方式三：JDK8开始新增的Lambda表达式。

 遍历方式：
 1、迭代器
 2、增强for
 3、foreach方法（结合Lambda）
 */
public class Demo03foreach {
    public static void main(String[] args) {
        Collection<String> c = new ArrayList<>();
        c.add("赵敏");
        c.add("小昭");
        c.add("殷素素");
        c.add("周芷若");
        System.out.println(c);

        // default void forEach(Consumer<? super T> action):  结合Lambda表达式遍历集合：
//        c.forEach(name -> System.out.println(name));
        c.forEach(System.out::println);


        Collection<Student> students = new ArrayList<>();
        students.add(new Student("兮", 20));
        students.add(new Student("鼬",18));
        System.out.println("students1111 = " + students);

        for (Student student : students) {
            System.out.println("student = " + student);
        }

        //匿名内部类
        students.forEach(new Consumer<Student>() {
            @Override
            public void accept(Student student) {
                System.out.println(student);
            }
        });

        //lambda
        students.forEach(student -> System.out.println(student)); //Lambda

        //方法引用
        students.forEach(System.out::println);
    }
}

class Student{
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}










